/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import models.LoginStorage;
import dao.DAO;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import java.lang.String;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Krzysiek
 */
public class AdminView extends javax.swing.JFrame {

    private LoginStorage model;
    private DAO dao = new DAO();

    public AdminView() {
        System.out.println("MainWindow");
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    setup();
                }
            });
        } catch (InterruptedException ex) {
            Logger.getLogger(AdminView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(AdminView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setModel(LoginStorage model) {
        this.model = model;
    }

    public LoginStorage getModel() {
        return this.model;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        tabs = new javax.swing.JTabbedPane();
        usersPane = new javax.swing.JScrollPane();
        usersTable = new javax.swing.JTable();
        acPane = new javax.swing.JScrollPane();
        acTable = new javax.swing.JTable();
        heatersPane = new javax.swing.JScrollPane();
        heatersTable = new javax.swing.JTable();
        lightsPane = new javax.swing.JScrollPane();
        lightsTable = new javax.swing.JTable();
        modifyButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("HAC System");

        usersTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "User Id", "Username", "Password", "AC", "Heaters", "Lights", "User Type"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        usersPane.setViewportView(usersTable);
        usersTable.getAccessibleContext().setAccessibleParent(usersPane);

        tabs.addTab("Users", usersPane);

        acTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Level", "Power"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        acPane.setViewportView(acTable);
        acTable.getAccessibleContext().setAccessibleParent(acPane);

        tabs.addTab("Air Conditioning", acPane);

        heatersTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Level", "Power"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        heatersPane.setViewportView(heatersTable);
        heatersTable.getAccessibleContext().setAccessibleParent(heatersPane);

        tabs.addTab("Heaters", heatersPane);

        lightsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Level", "Amount"
            }
        ));
        lightsPane.setViewportView(lightsTable);
        lightsTable.getAccessibleContext().setAccessibleParent(lightsPane);

        tabs.addTab("Lights", lightsPane);

        modifyButton.setText("Modify");

        deleteButton.setText("Delete");

        addButton.setText("Add");
        addButton.setToolTipText("");

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(addButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(modifyButton)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(tabs, javax.swing.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE))
                .addContainerGap())
        );

        mainPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addButton, deleteButton, modifyButton});

        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabs, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addButton)
                    .addComponent(deleteButton)
                    .addComponent(modifyButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public void setup() {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AdminView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AdminView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AdminView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AdminView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        initComponents();
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane acPane;
    private javax.swing.JTable acTable;
    private javax.swing.JButton addButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JScrollPane heatersPane;
    private javax.swing.JTable heatersTable;
    private javax.swing.JScrollPane lightsPane;
    private javax.swing.JTable lightsTable;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton modifyButton;
    private javax.swing.JTabbedPane tabs;
    private javax.swing.JScrollPane usersPane;
    private javax.swing.JTable usersTable;
    // End of variables declaration//GEN-END:variables

    public void addAddButtonListener(ActionListener addButtonListener) {
        addButton.addActionListener(addButtonListener);
    }

    public void addDeleteButtonListener(ActionListener deleteButtonListener) {
        deleteButton.addActionListener(deleteButtonListener);
    }

    public void addModifyButtonListener(ActionListener modifyButtonListener) {
        modifyButton.addActionListener(modifyButtonListener);
    }

    public void addJTabbedPaneListener(ChangeListener jTabbedPaneListener) {
        tabs.addChangeListener(jTabbedPaneListener);
    }

    public void setUsersTable(DefaultTableModel model) {
        this.usersTable.setModel(model);
    }

    public void setACTable(DefaultTableModel model) {
        this.acTable.setModel(model);
    }

    public void setHeatersTable(DefaultTableModel model) {
        this.heatersTable.setModel(model);
    }

    public void setLightsTable(DefaultTableModel model) {
        this.lightsTable.setModel(model);
    }

    public int getActiveTab() {
        return tabs.getSelectedIndex();
    }

    public String[] getSelectedRow() {
        JScrollPane pane = (JScrollPane) tabs.getSelectedComponent();
        JViewport viewport = pane.getViewport();
        JTable table = (JTable) viewport.getView();
        int row = table.getSelectedRow();
        if (row == -1) {
            row = 0;
        }
        int cols = table.getColumnCount();
        String[] data = new String[cols];
        for (int i = 1; i <= cols; i++) {
            data[i - 1] = table.getValueAt(row, i - 1).toString();
//            System.out.println(table.getValueAt(row, i - 1).toString());
        }
        return data;
    }
}
